<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
import { getMonthLineData} from "@/api/index";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '280px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
  },
  data() {
    return {
      chart: null
    }
  },

  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      getMonthLineData().then(res => {
        this.setOptions({
          userCount: res.data.map(a=>a.userCount),
          orderCount: res.data.map(a=>a.orderCount),
          monthData: res.data.map(a=>a.month)
        })
      });
    },
    setOptions({ userCount, orderCount, monthData } = {}) {
      this.chart.setOption({
        xAxis: {
          data: monthData,
          boundaryGap: false,
          axisTick: {
            show: false
          },
          axisLabel: {
            color: '#333333'
          }
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        yAxis: {
          axisTick: {
            show: false
          },
          axisLabel: {
            color: '#333333'
          }
        },
        legend: {
          data: ['新增用户', '每日订单'],
          align: 'right',
          right: '10'
        },
        series: [{
          name: '新增用户', itemStyle: {
            normal: {
              color: function (params) {
                // build a color map as your need.
                var colorList = [
                  '#C1232B', '#B5C334', '#FCCE10', '#E87C25', '#27727B',
                  '#FE8463', '#9BCA63', '#FAD860', '#F3A43B', '#60C0DD',
                  '#D7504B', '#C6E579', '#F4E001', '#F0805A', '#26C0C0'
                ];
                return colorList[params.dataIndex]
              },
              lineStyle: {
                color: '#fc9d22',
                width: 4
              }
            }
          },
          smooth: true,
          type: 'line',
          data: userCount,
          animationDuration: 1800,
          animationEasing: 'cubicInOut'
        },
        {
          name: '每日订单',
          smooth: true,
          type: 'line',
          itemStyle: {
            normal: {
              // color: '#3888fa',
              lineStyle: {
                color: '#3888fa',
                width: 4
              },
              areaStyle: {
                color: '#f3f8ff'
              }
            }
          },
          data: orderCount,
          animationDuration: 1800,
          animationEasing: 'quadraticOut'
        }]
      })
    }
  }
}
</script>
